﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace AAA.Forms.Components.Util
{
    public class AssignedValueComparer : IComparer<string>
    {
        private List<string> _lstRefSort;

        private char[] _cSplits;

        public char[] Splits
        {
            get { return _cSplits; }
            set { _cSplits = value; }
        }

        public string SplitString
        {
            set
            {
                Splits = value.ToCharArray();
            }
        }

        private int _iFieldIndex;
       
        public int FieldIndex
        {
          get { return _iFieldIndex; }
          set { _iFieldIndex = value; }
        }

        private bool _isLengthFirst;

        public bool IsLengthFirst
        {
            get { return _isLengthFirst; }
            set { _isLengthFirst = value; }
        }

        public AssignedValueComparer(List<string> lstRefSort)
            : this(lstRefSort, false)
        {
        }

        public AssignedValueComparer(List<string> lstRefSort, bool isLengthFirst)
            : this(lstRefSort, isLengthFirst, null, 0)
        {
        }

        public AssignedValueComparer(List<string> lstRefSort, char[] cSplits, int iFieldIndex)
            : this(lstRefSort, false, cSplits, iFieldIndex)
        {
        }

        public AssignedValueComparer(List<string> lstRefSort, bool isLengthFirst, char[] cSplits, int iFieldIndex)
        {
            _lstRefSort = lstRefSort;
            IsLengthFirst = isLengthFirst;
            Splits = cSplits;
            _iFieldIndex = iFieldIndex;
        }

        public int Compare(string x, string y)
        {
            string[] strValues;
            string strValue1 = (string)x;
            string strValue2 = (string)y;
            int iDiff;

            if(strValue1 != null)
            {
                strValues = strValue1.Split(Splits);
                strValue1 = strValues.Length > FieldIndex ? strValues[FieldIndex] : "";
            }

            if (strValue2 != null)
            {
                strValues = strValue2.Split(Splits);
                strValue2 = strValues.Length > FieldIndex ? strValues[FieldIndex] : "";
            }

            if (_lstRefSort == null)
            {
                iDiff = IsLengthFirst ? (strValue1.Length != strValue2.Length
                                            ? strValue1.Length - strValue2.Length
                                            : strValue1.CompareTo(strValue2))
                                      : strValue1.CompareTo(strValue2);
            }
            else
            {
                iDiff = _lstRefSort.IndexOf(strValue1 == null ? "" : strValue1) -
                        _lstRefSort.IndexOf(strValue2 == null ? "" : strValue2);
            }
            if (iDiff != 0)
                return iDiff;

            return 0;
        }
    }
}
